package com.ziroom.finance.rabbitmq.common.util;

import com.ziroom.finance.rabbitmq.common.base.CommonException;
import com.ziroom.finance.rabbitmq.common.enums.ResultCode;
import org.apache.http.util.TextUtils;

import java.util.Collection;

/**
 * 断言 copied from google Args
 * Created by duxiaokang on 16/8/29.
 */
public class Predict {

    public static void check(boolean expression, String message) throws CommonException {
        if (!expression) {
            doThrow(message);
        }
    }

    public static void check(boolean expression, String message, Object... args) throws CommonException {
        if (!expression) {
            doThrow(String.format(message, args));
        }
    }

    public static void check(boolean expression, String message, Object arg) throws CommonException {
        if (!expression) {
            doThrow(String.format(message, arg));
        }
    }

    public static <T> T notNull(T argument, String name) throws CommonException {
        if (argument == null) {
            doThrow(name + "不能为空");
        }
        return argument;
    }

    public static <T extends CharSequence> T notEmpty(T argument, String name) throws CommonException {
        if (argument == null) {
            doThrow(name + "不能为空");
        } else if (TextUtils.isEmpty(argument)) {
            doThrow(name + "不能为空");
        }
        return argument;
    }

    public static <T extends CharSequence> T notBlank(T argument, String name) throws CommonException {
        if (argument == null) {
            doThrow(name + "不能为空");
        } else if (TextUtils.isBlank(argument)) {
            doThrow(name + "不能为空");
        }
        return argument;
    }

    public static <T extends CharSequence> T containsNoBlanks(T argument, String name) throws CommonException {
        if (argument == null) {
            doThrow(name + "不能为空");
        } else if (TextUtils.containsBlanks(argument)) {
            doThrow(name + "不能包含空格");
        }
        return argument;
    }

    public static <E, T extends Collection<E>> T notEmpty(T argument, String name) throws CommonException {
        if (argument == null) {
            doThrow(name + "不能为空");
        } else if (argument.isEmpty()) {
            doThrow(name + "不能为空");
        }
        return argument;
    }

    public static int positive(int n, String name) throws CommonException {
        if (n <= 0) {
            doThrow(name + "必须大于0");
        }
        return n;
    }

    public static long positive(long n, String name) throws CommonException {
        if (n <= 0L) {
            doThrow(name + "必须大于0");
        }
        return n;
    }

    public static int notNegative(int n, String name) throws CommonException {
        if (n < 0) {
            doThrow(name + "必须大于等于0");
        }
        return n;
    }

    public static long notNegative(long n, String name) throws CommonException {
        if (n < 0L) {
            doThrow(name + "必须大于等于0");
        }

        return n;
    }

    /**
     * 抛出通用错误的工具方法
     * @author duxiaokang
     * @param message   error message
     * @throws CommonException  common exception
     */
    public static void doThrow(String message) throws CommonException {
        throw new CommonException(ResultCode.COMMON_ERROR, message);
    }
}
